﻿using JClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace JBot {
	class BotScripts {
		public static string[ ] scriptsList = Properties.Settings.Default.Scripts.Split('\n');
		private static string _method;
		private static Object[ ] _parms;

		public static void runScript(string path, string method, Object[ ] parms) {
			JScripts.CodeDom.Compiler.ScriptExecutor Script = new JScripts.CodeDom.Compiler.ScriptExecutor( );
			string[ ] references = new string[4];
			int j = 0;
			references[j] = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location);
			if (!references[j].EndsWith(@"\"))
				references[j] += @"\";
			references[j] += "agsXMPP.dll";
			j++;
			references[j] = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location);
			if (!references[j].EndsWith(@"\"))
				references[j] += @"\";
			references[j] += "JBotAPI.dll";
			j++;
			references[j] = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location);
			if (!references[j].EndsWith(@"\"))
				references[j] += @"\";
			references[j] += "JClientAPI.dll";
			j++;
			references[j] = System.IO.Path.GetDirectoryName(Application.ResourceAssembly.Location);
			if (!references[j].EndsWith(@"\"))
				references[j] += @"\";
			references[j] += "JScriptsAPI.dll";
			Script.ConnectionApiHandler = (App.xmpp as ConnectionProvider);
			Script.ScriptParms = parms;
			JScripts.CodeDom.Compiler.ScriptExetuceInfo SR = Script.RunScript(path, method, references);
			if (SR.RunFinish == false)
				for (int i = 0; i < SR.ScriptError.Count; i++)
					App.xmpp.Logs.Add(SR.ScriptError[i].ErrorText, JBotAPI.Logs.LogFlags.Compiler, JBotAPI.Logs.LogType.Error);
			else
				if (SR.CompilerResult.Errors.Count > 0)
					for (int i = 0; i < SR.CompilerResult.Errors.Count; i++)
						App.xmpp.Logs.Add(SR.CompilerResult.Errors[i].ToString( ), JBotAPI.Logs.LogFlags.Script, JBotAPI.Logs.LogType.Error);
		}
		public static void BW_DoWork(object sender, System.ComponentModel.DoWorkEventArgs ea) {
			string method = _method;
			Object[ ] parms = _parms;
			if (scriptsList != null && scriptsList.Length > 0)
				foreach (string path in scriptsList) {
					if (System.IO.File.Exists(path)) {
						try {
							runScript(path, method, parms);
						} catch (Exception e) {
							MessageBox.Show(e.ToString( ));
						}
					}
				}
		}

		public static void on_Start( ) {
			_parms = null;
			_method = "on_Start";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Connected( ) {
			_parms = null;
			_method = "on_Connected";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Load( ) {
			_parms = null;
			_method = "on_Load";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Unload( ) {
			_parms = null;
			_method = "on_Unload";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Presence(string uin, int status, string descr, string timer, string ip, int port) {
			_parms = new Object[6];
			_parms[0] = uin;
			_parms[1] = status;
			_parms[2] = descr;
			_parms[3] = timer;
			_parms[4] = ip;
			_parms[5] = port;
			_method = "on_Presence";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Message(string uin, string msg, string subject, string thread, DateTime time, int msg_class) {
			_parms = new Object[6];
			_parms[0] = uin;
			_parms[1] = msg;
			_parms[2] = subject;
			_parms[3] = thread;
			_parms[4] = time;
			_parms[5] = msg_class;
			_method = "on_Message";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Disconnected( ) {
			_parms = null;
			_method = "on_Disconnected";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
		public static void on_Stop( ) {
			_parms = null;
			_method = "on_Stop";
			System.ComponentModel.BackgroundWorker BW = new System.ComponentModel.BackgroundWorker( );
			BW.DoWork += new System.ComponentModel.DoWorkEventHandler(BW_DoWork);
			BW.RunWorkerAsync( );
		}
	}
}
